<template>
    <div style="padding: 15px;">
        <div style="margin-bottom: 10px;">
            <el-button @click="$router.push('/employee')">返回</el-button>
        </div>
        <el-form ref="employeeFormRef" style="max-width: 600px" :model="employeeForm" status-icon label-width="auto"
            class="demo-employeeForm">

            <el-form-item label="账号" prop="username">
                <el-input v-model="employeeForm.username" />
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input v-model="employeeForm.name" />
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="employeeForm.phone" />
            </el-form-item>
            <el-form-item label="身份证号" prop="idNumber">
                <el-input v-model="employeeForm.idNumber" />
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="employeeForm.sex">
                    <el-radio value="1">男</el-radio>
                    <el-radio value="0">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm()">
                    提交
                </el-button>
                <el-button @click="resetForm(employeeFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts" setup>

import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

import api from '@/api'

const router = useRouter();

const employeeFormRef = ref<FormInstance>()
const employeeForm = ref({
    name: '',
    idNumber:'',
    username: '',
    password: '',
    phone: '',
    sex: '1',
})

const submitForm = () => {
    console.log(employeeForm.value);
    api.AddEmployee(employeeForm.value).then(res => {
        console.log(res);
        if (res.data.code === 1) {
            ElMessage({
                message: '添加成功',
                type: 'success',
            })
            router.push('/employee')
        } else {
            ElMessage({
                message: res.data.msg,
                type: 'error',
            })
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
